using System;
using System.Collections.Generic;
using System.Text;
using TidhiGanitam.DataModel;
namespace TidhiGanitam.Ganitam
{
    public class Abdapudu
    {
        int m_EnglishYear;
        int m_SalivahanaYear;
        VaramTidhiItem m_VaramTidhiSet;
        Varam m_ChalanaPhalam;
        Enums.ChalanaPhalam m_ChalanaphamSign;
        Pradesam m_IstaPradesam;
        bool m_SamskaramDone;
        double m_Tidhulu;
        DateTime m_UgadiDate;

        public Abdapudu(int EnglishYear, Pradesam IstaPradesam, DateTime Ugadi)
        {
            m_EnglishYear = EnglishYear;
            m_IstaPradesam = new Pradesam(IstaPradesam);
            m_SalivahanaYear = EnglishYear - Constants.YearBaseDifference;
            m_UgadiDate = Ugadi;
            m_VaramTidhiSet = new VaramTidhiItem();
            m_ChalanaPhalam = new Varam();
            m_ChalanaphamSign = Enums.ChalanaPhalam.None;
            m_SamskaramDone = false;
            m_Tidhulu = 0;
        }
        public DateTime UgadiDate
        {
            get { return m_UgadiDate; }
        }
        public double Tidhulu
        {
            get { return m_Tidhulu; }
        }
        public bool SamskaramDone
        {
            get
            {
                return m_SamskaramDone;
            }
        }
        public int EnglishYear
        {
            get
            {
                return m_EnglishYear;
            }
        }
        public int SalivahanaYear
        {
            get
            {
                return m_SalivahanaYear;
            }
        }
        public Pradesam IstaPradesam
        {
            get { return m_IstaPradesam; }
        }
        public Varam ChalanaPhalam
        {
            get {
                return m_ChalanaPhalam;
               //return new Rasi(0, m_ChalanaPhalam.Ghadiyalu, m_ChalanaPhalam.Vighadiyalu, m_ChalanaPhalam.Paralu);
                }
        }
        public Enums.ChalanaPhalam ChalanaPhalamSign
        {
            get { return m_ChalanaphamSign; }
        }
        public VaramTidhiItem VaramTidhi
        {
            get { return m_VaramTidhiSet; }
        }
        public void DoAbdapaSamskaram()
        {
            /********************************************
             * 1. First Calculate chalanphalam, Tidhulu for the salivahanayear
             * 2. Dinasamskaram for Chalanaphalam
             * 3. Istapradesa samskaram for Chalanaphalam
             * 4. Tidhisamskaram
             * *****************************************/
            m_VaramTidhiSet = SaliVahanaSakam.GetVaramTidhiSet(m_SalivahanaYear);
            DinardhaSamskaram();
            IstaPradesaSamskaram();
            TidhiSamskaram();
            m_SamskaramDone = true;
        }
        private void DinardhaSamskaram()
        {
            //1. Dinaradha samskaram is done for varam
            //2. if Ghadiya, Vighadiyalu in the varam are greater than dinardham,
            //   subtract dinardham from varam. we leaveout varam part here.
            //   This is called Runa Chalana Phalam
            //3. if Ghadiya, Vighadiyalu in the varam are less than dinardham,
            //   subtract varam from dinardham. 
            //   This is called Dhana Chalana Phalam
            
            //we leaveout the varam part here.
            Varam ghadiyaVighadiyaPart = new Varam(0,
                                    m_VaramTidhiSet.VaramItem.Ghadiyalu,
                                    m_VaramTidhiSet.VaramItem.Vighadiyalu);

            if (ghadiyaVighadiyaPart > Constants.Dinardham)
            {
                m_ChalanaPhalam = ghadiyaVighadiyaPart - Constants.Dinardham;
                m_ChalanaphamSign = Enums.ChalanaPhalam.Runam;
            }
            else
            {
                m_ChalanaPhalam = Constants.Dinardham - ghadiyaVighadiyaPart;
                m_ChalanaphamSign = Enums.ChalanaPhalam.Dhanam;
            }
        }
        private void IstaPradesaSamskaram()
        {
            /**********************************************
             * 1. All the values are calculate for bombay
             * 2. We need to change these values to the istapradesam.
             * 3. Subtract the longitude of the istapradesam if the place 
             *                                          is East to bombay
             * 4. Add the longitude of the istapradesam if the place is 
             *                                          West to bombay
             * ********************************************/
            if (m_IstaPradesam.Position == Enums.PradesaPosition.East)
            {
                if (m_ChalanaphamSign == Enums.ChalanaPhalam.Runam)
                {
                    m_ChalanaPhalam = m_ChalanaPhalam + m_IstaPradesam.Longitude;
                }
                else
                {
                    if (m_ChalanaPhalam > m_IstaPradesam.Longitude)
                    {
                        m_ChalanaPhalam = m_ChalanaPhalam - m_IstaPradesam.Longitude;
                    }
                    else
                    {
                        m_ChalanaPhalam = m_IstaPradesam.Longitude - m_ChalanaPhalam;
                    }
                }
            }
            else
            {
                if (m_ChalanaphamSign == Enums.ChalanaPhalam.Dhanam)
                {
                    m_ChalanaPhalam = m_ChalanaPhalam + m_IstaPradesam.Longitude;
                }
                else
                {
                    if (m_ChalanaPhalam > m_IstaPradesam.Longitude)
                    {
                        m_ChalanaPhalam = m_ChalanaPhalam - m_IstaPradesam.Longitude;
                    }
                    else
                    {
                        m_ChalanaPhalam = m_IstaPradesam.Longitude - m_ChalanaPhalam;
                    }
                }
            }


        }
        private void TidhiSamskaram()
        {
            /*****************************************************
             * 1. If Ghadiyalu part in Tidhulu is > 45, We take the next 
             *    number as tidhulu for calculation.
             * **************************************************/
            m_Tidhulu = m_VaramTidhiSet.TidhiItem.Tidhulu;
            Tidhi tidhiPart = new Tidhi(0, m_VaramTidhiSet.TidhiItem.Ghadiyalu);
            if (tidhiPart > Constants.TidhiConsideration)
            {
                m_Tidhulu++;
            }

        }
    }
}
